#bilibili用户系统

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUiType
from index import *
import enter_ui
import tu_rc
import sys

# UI--Logic分离
ui, _ = loadUiType('main.ui')

class MainApp(QMainWindow,ui):

    #定义构造方法
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.handle_ui_change()
        self.handle_buttons()
        self.show_attention()
        self.show_animation()
        self.show_collect()


    #UI变化处理
    def handle_ui_change(self):
        self.all_tabWidget.tabBar().setVisible(False)

    #所有Button的消息与槽的通信
    def handle_buttons(self):
        self.guanzhuButton.clicked.connect(self.open_guanzhu_tab)
        self.zhuifanButton.clicked.connect(self.open_zhuifan_tab)
        self.shoucangButton.clicked.connect(self.open_shoucang_tab)
        self.add_up_Button.clicked.connect(self.add_attention)
        self.add_fan_Button.clicked.connect(self.add_animation)
        self.add_video_Button.clicked.connect(self.add_collect)
        self.editor_up_find_Button.clicked.connect(self.search_attention)
        self.editor_up_Button.clicked.connect(self.editor_attention)
        self.cut_up_Button.clicked.connect(self.delete_attention)
        self.editor_fan_find_Button.clicked.connect(self.search_animation)
        self.editor_fan_Button.clicked.connect(self.editor_animation)
        self.cut_fan_Button.clicked.connect(self.delete_animation)
        self.editor_video_find_Button.clicked.connect(self.search_collect)
        self.editor_video_Button.clicked.connect(self.editor_collect)
        self.cut_video_Button.clicked.connect(self.delete_collect)
        self.tuichuButton.clicked.connect(self.back)


    def back(self):
        reply = QMessageBox.question(self, '确认', '确认登出？', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            self.enter_app = enter_ui.EnterApp()
            self.close()
            self.enter_app.show()



    #选项卡联动
    def open_guanzhu_tab(self):
        self.all_tabWidget.setCurrentIndex(0)

    def open_zhuifan_tab(self):
        self.all_tabWidget.setCurrentIndex(1)

    def open_shoucang_tab(self):
        self.all_tabWidget.setCurrentIndex(2)

    #数据库的处理
    #添加关注
    def add_attention(self):
        #获取连接
        conn = get_conn()
        #获取cursor
        cur = conn.cursor()
        #sql语句
        sql = "insert into attention(" \
              "attention_id,attention_name,attention_sign,attention_collect,attention_send,attention_view)" \
              " values(%s,%s,%s,%s,%s,%s)"
        attention_id = self.add_uid_Edit.text()
        attention_name = self.add_name_Edit.text()
        attention_sign = self.add_sign_Edit.text()
        attention_collect = self.add_collect_Edit.toPlainText()
        attention_send = self.add_send_Edit.toPlainText()
        attention_view = self.add_view_Edit.toPlainText()

        #执行语句
        values = (attention_id,attention_name,attention_sign,attention_collect,attention_send,attention_view)
        cur.execute(sql, values)
        # 5、insert、update、delete必须显示提交
        conn.commit()
        # 6、关闭资源
        close_conn(conn, cur)
        # 7、消息提示
        self.statusBar().showMessage('关注成功！')
        self.clearEdit_up_1()
        self.show_attention()

    def clearEdit_up_1(self):
        self.add_uid_Edit.clear()
        self.add_name_Edit.clear()
        self.add_sign_Edit.clear()
        self.add_collect_Edit.clear()
        self.add_send_Edit.clear()
        self.add_view_Edit.clear()


# 显示已有数据，并且添加完毕后直接可以看到
    def show_attention(self):
        conn = get_conn()
        cur = conn.cursor()
        sql = "select * from attention"
        cur.execute(sql)
        data = cur.fetchall()

        if data:
            self.tableWidget.setRowCount(0)
            self.tableWidget.insertRow(0)
            for row, form in enumerate(data):
                for column, item in enumerate(form):
                    self.tableWidget.setItem(row, column, QTableWidgetItem(str(item)))
                    column += 1

                row_position = self.tableWidget.rowCount()
                self.tableWidget.insertRow(row_position)

    # 查询关注
    def search_attention(self):
        conn = get_conn()
        cur = conn.cursor()
        sql = "select * from attention where attention_id = %s"
        attention_id = self.editor_uid_Edit.text()
        cur.execute(sql,(attention_id,))
        data = cur.fetchone()
        if data:
            self.editor_uid_Edit.setText(data[0])
            self.editor_name_Edit.setText(data[1])
            self.editor_sign_Edit.setText(data[2])
            self.editor_collect_Edit.setPlainText(data[3])
            self.editor_send_Edit.setPlainText(data[4])
            self.editor_view_Edit.setPlainText(data[5])

        else:
            self.statusBar().showMessage('你没有关注这个up')
            self.clearEdit_up_2()

    def clearEdit_up_2(self):
        self.editor_uid_Edit.clear()
        self.editor_name_Edit.clear()
        self.editor_sign_Edit.clear()
        self.editor_collect_Edit.clear()
        self.editor_send_Edit.clear()
        self.editor_view_Edit.clear()


    # 编辑up信息
    '''
    def editor_attention(self):
        # 1、获取连接
        conn = get_conn()
        # 2、获取cursor
        cur = conn.cursor()
        # 3、SQl语句
        sql = "update attention set " \
              "attention_name = %s,attention_sign = %s,attention_collect = %s,attention_send = %s,attention_view = %s" \
              " where attention_id=%s"
        attention_id = self.editor_uid_Edit.text()
        attention_name = self.editor_name_Edit.text()
        attention_sign = self.editor_sign_Edit.text()
        attention_collect = self.editor_collect_Edit.toPlainText()
        attention_send = self.editor_send_Edit.toPlainText()
        attention_view = self.editor_view_Edit.toPlainText()
        if attention_id == "":
            self.statusBar().showMessage('uid不能为空！！')
            self.clearEdit_up_2()
            return
        else:
            # 4、执行语句
            values = (attention_name, attention_sign, attention_collect, attention_send, attention_view,attention_id,)
            cur.execute(sql, values)
            # 5、insert、update、delete必须显示提交
            conn.commit()
            self.show_attention()
            self.statusBar().showMessage('修改成功！')
            self.clearEdit_up_2()
        return
        '''


    # 编辑up信息
    def editor_attention(self):
        attention_id = self.editor_uid_Edit.text()
        attention_name = self.editor_name_Edit.text()
        attention_sign = self.editor_sign_Edit.text()
        attention_collect = self.editor_collect_Edit.toPlainText()
        attention_send = self.editor_send_Edit.toPlainText()
        attention_view = self.editor_view_Edit.toPlainText()
       # new_quantity = quantity
        # 获取连接
        conn = get_conn()
        # 获取cursor
        cur = conn.cursor()
        # SQL语句
        sql = "update attention set " \
              "attention_name = %s,attention_sign = %s,attention_collect = %s,attention_send = %s,attention_view = %s" \
              " where attention_id = %s"
        # 执行语句
        values = (attention_name, attention_sign, attention_collect, attention_send, attention_view,attention_id)
        cur.execute(sql, values)
        #cur.execute(sql2,(new_quantity,goodsname))
        # insert,update,delete必须显示提交
        conn.commit()
        # 关闭资源
        close_conn(conn, cur)
        # 消息提示
        self.show_attention()
        self.statusBar().showMessage('修改信息成功！')
        self.clearEdit_up_2()



    # 取消关注
    def delete_attention(self):
        # 1、获取连接
        conn = get_conn()
        # 2、获取cursor
        cur = conn.cursor()
        sql = "delete from attention where attention_id = %s"
        attention_id = self.editor_uid_Edit.text()
        if attention_id == "":
            self.statusBar().showMessage('uid不能为空！！')
        else :
            reply = QMessageBox.question(self, '确认', '确定取消关注?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if reply == QMessageBox.Yes:
                cur.execute(sql, (attention_id,))
                conn.commit()
                close_conn(conn, cur)
                self.show_attention()
                self.statusBar().showMessage('取消关注！')
                self.clearEdit_up_2()



    #添加追番
    def add_animation(self):
        #获取连接
        conn = get_conn()
        #获取cursor
        cur = conn.cursor()
        #sql语句
        sql = "insert into animation(" \
              "animation_name,animation_types,animation_cv,animation_mader,animation_intro)" \
              " values(%s,%s,%s,%s,%s)"
        animation_name = self.add_fan_Edit.text()
        animation_types = self.add_feel_Edit.text()
        animation_cv = self.add_star_Edit.toPlainText()
        animation_mader = self.add_mader_Edit.toPlainText()
        animation_intro = self.add_information_Edit.toPlainText()

        #执行语句
        values = (animation_name,animation_types,animation_cv,animation_mader,animation_intro)
        cur.execute(sql, values)
        # 5、insert、update、delete必须显示提交
        conn.commit()
        # 6、关闭资源
        close_conn(conn, cur)
        # 7、消息提示
        self.statusBar().showMessage('自己追的番要记得看完哦！')
        self.clearEdit_fan()
        self.show_animation()

    def clearEdit_fan(self):
        self.add_fan_Edit.clear()
        self.add_feel_Edit.clear()
        self.add_star_Edit.clear()
        self.add_mader_Edit.clear()
        self.add_information_Edit.clear()



# 显示已有数据，并且添加完毕后直接可以看到
    def show_animation(self):
        conn = get_conn()
        cur = conn.cursor()
        sql = "select * from animation"
        cur.execute(sql)
        data = cur.fetchall()

        if data:
            self.tableWidget_2.setRowCount(0)
            self.tableWidget_2.insertRow(0)
            for row, form in enumerate(data):
                for column, item in enumerate(form):
                    self.tableWidget_2.setItem(row, column, QTableWidgetItem(str(item)))
                    column += 1

                row_position = self.tableWidget_2.rowCount()
                self.tableWidget_2.insertRow(row_position)

    # 查询番剧
    def search_animation(self):
        conn = get_conn()
        cur = conn.cursor()
        sql = "select * from animation where animation_name = %s"
        animation_name = self.editor_fan_Edit.text()
        cur.execute(sql, (animation_name,))
        data = cur.fetchone()
        if data:
            self.editor_fan_Edit.setText(data[0])
            self.editor_feel_Edit.setText(data[1])
            self.editor_star_Edit.setPlainText(data[2])
            self.editor_mader_Edit.setPlainText(data[3])
            self.editor_information_Edit.setPlainText(data[4])

        else:
            self.statusBar().showMessage('你没有追番！')
            self.clearEdit_fan_2()

    def clearEdit_fan_2(self):
        self.editor_fan_Edit.clear()
        self.editor_feel_Edit.clear()
        self.editor_star_Edit.clear()
        self.editor_mader_Edit.clear()
        self.editor_information_Edit.clear()

    # 编辑番剧信息
    def editor_animation(self):
        # 1、获取连接
        conn = get_conn()
        # 2、获取cursor
        cur = conn.cursor()
        # 3、SQl语句
        sql = "update animation set " \
              "animation_types = %s,animation_cv = %s,animation_mader = %s,animation_intro = %s" \
              " where animation_name=%s"
        animation_name = self.editor_fan_Edit.text()
        animation_types = self.editor_feel_Edit.text()
        animation_cv = self.editor_star_Edit.toPlainText()
        animation_mader = self.editor_mader_Edit.toPlainText()
        animation_intro = self.editor_information_Edit.toPlainText()
        if animation_name == "":
            self.statusBar().showMessage('uid不能为空！！')
            self.clearEdit_fan_2()
            return
        else:

            # 4、执行语句
            values = (animation_types, animation_cv, animation_mader, animation_intro,animation_name)
            cur.execute(sql, values)
            # 5、insert、update、delete必须显示提交
            conn.commit()
            self.show_animation()
            self.statusBar().showMessage('修改信息成功！')
            self.clearEdit_fan_2()
        return

    # 取消追番
    def delete_animation(self):
        # 1、获取连接
        conn = get_conn()
        # 2、获取cursor
        cur = conn.cursor()
        sql = "delete from animation where animation_name = %s"
        animation_name = self.editor_fan_Edit.text()
        if animation_name == "":
            self.statusBar().showMessage('uid不能为空！！')
        else:
            reply = QMessageBox.question(self, '确认', '确定取消追番?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if reply == QMessageBox.Yes:
                cur.execute(sql, (animation_name,))
                conn.commit()
                close_conn(conn, cur)
                self.show_animation()
                self.statusBar().showMessage('取消关注！')
                self.clearEdit_fan_2()




    #添加收藏
    def add_collect(self):
        #获取连接
        conn = get_conn()
        #获取cursor
        cur = conn.cursor()
        #sql语句
        sql = "insert into collect(" \
              "collect_num,collect_name,collect_mader,collect_area,collect_time,collect_zan,collect_coins,collect_collection,collect_infor)" \
              " values(%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        collect_num = self.add_video_num_Edit.text()
        collect_name = self.add_video_name_Edit.text()
        collect_mader = self.add_video_mader_Edit.text()
        collect_area = self.add_video_area_Edit.text()
        collect_time = self.add_video_time_Edit.text()
        collect_zan = self.add_video_dianzan_Edit.text()
        collect_coins = self.add_video_toubi_Edit.text()
        collect_collection = self.add_video_shoucang_Edit.text()
        collect_infor = self.add_video_information_Edit.toPlainText()


        #执行语句
        values = (collect_num,collect_name,collect_mader,collect_area,collect_time,collect_zan,collect_coins,collect_collection,collect_infor)
        cur.execute(sql, values)
        # 5、insert、update、delete必须显示提交
        conn.commit()
        # 6、关闭资源
        close_conn(conn, cur)
        # 7、消息提示
        self.statusBar().showMessage('成功加入收藏夹！')
        self.clearEdit_collect()
        self.show_collect()

    def clearEdit_collect(self):
        self.add_video_num_Edit.clear()
        self.add_video_name_Edit.clear()
        self.add_video_mader_Edit.clear()
        self.add_video_area_Edit.clear()
        self.add_video_time_Edit.clear()
        self.add_video_dianzan_Edit.clear()
        self.add_video_toubi_Edit.clear()
        self.add_video_shoucang_Edit.clear()
        self.add_video_information_Edit.clear()



# 显示已有数据，并且添加完毕后直接可以看到
    def show_collect(self):
        conn = get_conn()
        cur = conn.cursor()
        sql = "select * from collect"
        cur.execute(sql)
        data = cur.fetchall()

        if data:
            self.tableWidget_3.setRowCount(0)
            self.tableWidget_3.insertRow(0)
            for row, form in enumerate(data):
                for column, item in enumerate(form):
                    self.tableWidget_3.setItem(row, column, QTableWidgetItem(str(item)))
                    column += 1

                row_position = self.tableWidget_3.rowCount()
                self.tableWidget_3.insertRow(row_position)


    # 查询收藏夹
    def search_collect(self):
        conn = get_conn()
        cur = conn.cursor()
        sql = "select * from collect where collect_num = %s"
        collect_num = self.editor_video_num_Edit.text()
        cur.execute(sql, (collect_num,))
        data = cur.fetchone()
        if data:
            self.editor_video_num_Edit.setText(data[0])
            self.editor_video_name_Edit.setText(data[1])
            self.editor_video_mader_Edit.setText(data[2])
            self.editor_video_area_Edit.setText(data[3])
            self.editor_video_time_Edit.setText(data[4])
            self.editor_video_dianzan_Edit.setText(data[5])
            self.editor_video_toubi_Edit.setText(data[6])
            self.editor_video_shoucang_Edit.setText(data[7])
            self.editor_video_information_Edit.setPlainText(data[8])


        else:
            self.statusBar().showMessage('你没有收藏！')
            self.clearEdit_view_2()

    def clearEdit_view_2(self):
        self.editor_video_num_Edit.clear()
        self.editor_video_name_Edit.clear()
        self.editor_video_mader_Edit.clear()
        self.editor_video_area_Edit.clear()
        self.editor_video_time_Edit.clear()
        self.editor_video_dianzan_Edit.clear()
        self.editor_video_toubi_Edit.clear()
        self.editor_video_shoucang_Edit.clear()
        self.editor_video_information_Edit.clear()

    # 编辑收藏视频信息
    def editor_collect(self):
        # 1、获取连接
        conn = get_conn()
        # 2、获取cursor
        cur = conn.cursor()
        # 3、SQl语句
        sql = "update collect set " \
              "collect_name = %s,collect_mader = %s,collect_area = %s,collect_time = %s," \
              "collect_zan = %s,collect_coins = %s,collect_collection = %s,collect_infor = %s \
              " "where collect_num=%s"
        collect_num = self.editor_video_num_Edit.text()
        collect_name = self.editor_video_name_Edit.text()
        collect_mader = self.editor_video_mader_Edit.text()
        collect_area = self.editor_video_area_Edit.text()
        collect_time = self.editor_video_time_Edit.text()
        collect_zan = self.editor_video_dianzan_Edit.text()
        collect_coins = self.editor_video_toubi_Edit.text()
        collect_collection = self.editor_video_shoucang_Edit.text()
        collect_infor = self.editor_video_information_Edit.toPlainText()

        if collect_num == "":
            self.statusBar().showMessage('uid不能为空！！')
            self.clearEdit_view_2()
            return
        else:
            # 4、执行语句
            values = (collect_name,collect_mader,collect_area,collect_time,collect_zan,
                      collect_coins,collect_collection,collect_infor,collect_num)
            cur.execute(sql, values)
            # 5、insert、update、delete必须显示提交
            conn.commit()
            self.show_collect()
            self.statusBar().showMessage('修改信息成功！')
            self.clearEdit_view_2()
        return

    # 取消收藏
    def delete_collect(self):
        # 1、获取连接
        conn = get_conn()
        # 2、获取cursor
        cur = conn.cursor()
        sql = "delete from collect where collect_num = %s"
        collect_num = self.editor_video_num_Edit.text()
        if collect_num == "":
            self.statusBar().showMessage('视频号不能为空！！')
        else:
            reply = QMessageBox.question(self, '确认', '确定取消收藏?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if reply == QMessageBox.Yes:
                cur.execute(sql, (collect_num,))
                conn.commit()
                close_conn(conn, cur)
                self.show_collect()
                self.statusBar().showMessage('成功移除收藏夹！')
                self.clearEdit_view_2()




def main():
    app = QApplication([])
    window = MainApp()
    window.show()
    app.exec_()


if __name__ == '__main__':
    main()
